#include "head_s.h"

int read_stdin(user_t* status, portocol_t* portocol, int net_fd) {
    //printf("readstdin\n");
    int ret_recv = recv(net_fd, portocol, sizeof(portocol_t), MSG_WAITALL);
    //printf("%s\n", portocol->arg);
    ERROR_CHECK(ret_recv, -1, "read_stdin 4 recv");
    
    //有两个参数的情况
    if(portocol->arg_num == 2) {
        int arg_sub = 0;
        //分割用户名
        int user_name_sub = 0;
        while(portocol->arg[arg_sub] != '&') {
            status->user_name[user_name_sub] = portocol->arg[arg_sub];
            arg_sub++;
            user_name_sub++;
        }
        //分割路径
        arg_sub++;
        int path_sub = 0;
        while(portocol->arg[arg_sub] != '\0') {
            status->path[path_sub] = portocol->arg[arg_sub];
            arg_sub++;
            path_sub++;
        }
    }
    //有三个参数的情况
    else if(portocol->arg_num == 3){
        int arg_sub = 0;
        //分割用户名
        int user_name_sub = 0;
        while(portocol->arg[arg_sub] != '&') {
            status->user_name[user_name_sub] = portocol->arg[arg_sub];
            arg_sub++;
            user_name_sub++;
        }

        arg_sub++;
        //分割出来路径
        int path_sub = 0;
        while(portocol->arg[arg_sub] != '&') {
            status->path[path_sub] = portocol->arg[arg_sub];
            arg_sub++;
            path_sub++;
        }

        arg_sub++;
        //分割出来命令
        int user_arg_sub = 0;
        while(portocol->arg[arg_sub] != '\0') {
            status->arg[user_arg_sub] = portocol->arg[arg_sub];
            arg_sub++;
            user_arg_sub++;
        }
        //printf("read: user_name = %s, path = %s, mima = %s\n", status->user_name, status->path, status->arg);
    }

    return ret_recv;
}
